////////////////////////////////////////////////////////////////////////////////////
// File:        TNodBase.h
// Purpose:     Class which download licenses and send an event when all licenses
//              have been downloaded
// Author:      Tukero
// Copyright:   (c) Tukero <tukero@gmail.com>
// Licence:     GNU/LGPL v3
////////////////////////////////////////////////////////////////////////////////////

#ifndef _TNOD_BASE_H_
#define _TNOD_BASE_H_

#ifndef WX_PRECOMP
#  include <wx/wx.h>
#else
#  include <wx/wxprec.h>
#endif

#include "Version.h"
#include "EsetInfo.h"
#include "MultiLanguage.h"
#include "PassServers.h"
#include "NewVersionMsg.h"

WX_DECLARE_LIST(wxThread, LinkListThreads);

class TNodBase abstract : public wxThread
{
protected:
    LinkListLicense m_ValidLicenses;
    LinkListLicense m_CheckedLicenses;
    LinkListThreads m_ListOfLicenseThreads;
    
    EsetInfo        *m_EsetInfo;
    wxCommandEvent  *m_DownloadEvent;
    wxEvtHandler    *m_HandlerForEvent;
    int              m_EventType;

    NewVersionMsg   *m_NewVersionMsg;
    
    void**          m_endIndicator;
    bool            m_IsMain;
    bool            m_ProcessNewVersionMsg;
    bool            m_MsgWithNewVersionOnly;
public:    
    enum
    {
        EVENT_GOTLICENSE = 1,
        EVENT_GOTALLLICENSES
    };
    TNodBase();
    
    unsigned int    m_Actions2Do;

    //void        SetProcessNewVersionMsg(bool value);
    //void            SetMsgWithNewVersionOnly(bool value);
    void            SetProcessNewVersionMsg(bool process, bool MsgWithNewVersionOnly = false);

    // Action constants
    enum
    {
        CHECK_UPDATES = 0x4,
        GET_LICENSE = 0x08,
        ALL_ACTIONS = CHECK_UPDATES | GET_LICENSE
    };    
    
    void            SetEndIndicator(void **indicator);
    void            SetEventReceiver(wxEvtHandler* handlerForEvent, wxCommandEvent* event2Send, int eventType);
    virtual void    Start();
    static  bool    IsValidLicense(License &license);
    void            FindLicenses(LinkListLicense *validLicenses,License *excludedLicenses = NULL, int* totalValidServers = NULL);
    void            CheckValidityOfLicenses(PassServer *server, LinkListLicense *validLicenses,License *excludedLicense , int maxValidLicenses = 1);
    virtual void    CheckUpdates();
    //virtual void    SendEvent(int type);
    void            SetAsMain();
    void            ProcessNewVersionMsg(NewVersionMsg* newVersion);
    virtual void    RemoveChildThread(wxThread *licenseChecking);
    virtual void    DeleteChildThreads();
    virtual void    CreateLicenseThreads(LinkListLicense *licenses4Checking, unsigned int actions, LinkListLicense *validLicenses = NULL, License *excludedLicense = NULL, int maxValidLicenses = -1, bool licWithNoExpirationOnly = false);
    virtual void    CheckLicensesList(PassServer *server, LinkListLicense *validLicenses, License * excludedLicense);

    virtual void*   Entry();
    virtual void    OnExit();

    virtual         ~TNodBase() { }
};

#endif